﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using AForge.Imaging;
using AForge.Imaging.Filters;
using BrainTechLLC;
using BrainTechLLC.ImageOperators;

namespace BrainTechLLC.FilterExtensions
{
    /// <summary>
    /// These extension methods are used to convert possibly unusable image formats into image formats that can be used by specific filters
    /// </summary>
    public static class ConvertFormatExtensions
    {
        public static bool TryConvertToUsableFormat(this System.Drawing.Imaging.PixelFormat format, ref UnmanagedImage dstImg, ref UnmanagedImage tmpImg)
        {
            switch (format)
            {
                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                    Debug.WriteLine("Converting from " + dstImg.PixelFormat.ToString());
                    tmpImg = dstImg;
                    dstImg = (new AForge.Imaging.Filters.GrayscaleBT709()).Apply(tmpImg);
                    tmpImg.Dispose();
                    Debug.WriteLine("Converted to " + dstImg.PixelFormat.ToString());
                    return true;
                case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                    Debug.WriteLine("Converting from " + dstImg.PixelFormat.ToString());
                    tmpImg = dstImg;
                    dstImg = (new AForge.Imaging.Filters.GrayscaleToRGB()).Apply(tmpImg);
                    tmpImg.Dispose();
                    Debug.WriteLine("Converted to " + dstImg.PixelFormat.ToString());
                    return true;
            }
            return false;
        }
    }
}